<?php
$key = $form_view_config['key'];
$select_config = $form_view_config['select_config'];
if(!empty($select_config['sub_select_config'])){
	$sub_select_config = $select_config['sub_select_config'];
?>
<script type="text/javascript">
	var is_called = <?=($id > 0) ? "true" : "false"?>;
	function ajax_get_sub_select(value){
		$.ajax({
			url: root+module+'/ajax_get_sub_select',
			type: 'post',
			data: {
			<?php 
			foreach($sub_select_config as $k=>$v) { ?>
				<?=$k?>: '<?=$v?>',
			<?php 
			}
			?>
				value: value,
				flag: is_called
			},
			success: function(response){
				if(response){
					if(!is_called){
						$('#field_select_<?=$form_view_config['name']?>').after(response);
						is_called = true;
					} else {
						$('#field_select_<?=$sub_select_config['name']?>').html(response);
					}
				}
			}
		});
	}
</script>
<?php 
}
?>
<div id="field_select_<?=$form_view_config['name']?>">
	<div class="row_text_field" >
		<table cellspacing="0" cellpadding="0" border="0" width="100%">
			<tr>
				<td class="left_text_field"><?=$form_view_config['text']?></td>
				<td class="right_text_field">
					<select onChange="<?=!empty($sub_select_config)? 'ajax_get_sub_select($(this).val())' : ''?>" name="<?=$form_view_config['name']?>" id="<?=$form_view_config['name']?>">
						<option value="0"><?=$select_config['select_default_text']?></option>
						<?php
						if(is_array($select_options)){
							foreach($select_options as $value){
								$selected = (!empty($result->$key) && $result->$key == $value->id) ? 'selected="selected"' : '';
						?>
						<option <?=$selected?> value="<?=$value->id?>"><?=$value->$select_config['referred_field_name']?></option>
						<?php 
							}
						} else {
							echo $select_options;
							if(isset($result->$key)){ //use script to set selected option
						?>
							<script type="text/javascript">
								$('#<?=$form_view_config['name']?> option[value=<?=$result->$key?>]').attr('selected', 'selected');
							</script>
						<?php 
							}
						}
						?>
					</select>
				</td>
			</tr>
		</table>
	</div>
 </div>
<?php
if(!empty($id) && !empty($sub_select_config)){
	$parent_id = $result->$form_view_config['name'];
	$data = admin_build_sub_select_config($sub_select_config,$parent_id);
	echo $CI->form_field_build_field($data, $result);
}
?>